<?php

class IndexAction extends CommonAction {

    public function index() {
		$userInfo = M('user')->where('id='.$_SESSION['m_user']['id'])->find();
		$this->assign('jifen',$userInfo['jifen']);
        $this->display();
    }

    public function step1() {
		if(isset($_POST['step']))
		{
			for($i=0;$i<$_POST['times'];$i++)
			{
				$data = $this->gopay($_POST['phone'],$_POST['money']);
				M('phone')->add(array('user_id'=>$_SESSION['m_user']['id'],'phone'=>$_POST['phone'],'order_amount'=>$data['pay_amount'],'order_info'=>$data['pay_params'],'status'=>0));
			}
			$this->redirect('Ucenter/order');
		}else{
			$this->assign('info', $_POST);
			$this->display();
		}
        
    }

   private function gopay($phone,$money) {

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, 'http://epay.uc.cn/index.php?do=mobile_input&uc_param_str=nidnbifrvepfip');
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt ($ch, CURLOPT_HEADER, false); 
		$data = 'mobile_num=' . $phone . '&amount='.$money.'&payer_id=2001';
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$tmp = curl_exec($ch);
		$tmp = json_decode($tmp);
		curl_close($ch);
		//echo json_encode($tmp->data->pay_params);

		return array('pay_amount'=>$tmp->data->pay_amount,'pay_params'=>$tmp->data->pay_params);
    }
    
    function handle_jifen()
    {
        $User = M('user');
        $userInfo = $User->where('id='.$_SESSION['m_user']['id'].' and jifen >= '.$_POST['jifen'])->find();

        if($userInfo)
        {
            M('user')->where(array('id'=>$_SESSION['m_user']['id']))->setDec('jifen',$_POST['jifen']);
            echo 1;
        }else{
            echo 2;
        }
        die;
    }

}